import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {HttpClient} from '@angular/common/http';
import {ActivatedRoute, Router} from '@angular/router';
@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  loginForm: FormGroup;
  account: string;
  imgSrc: string;
  url = 'http://127.0.0.1:8080/lx/';

  constructor(private fb: FormBuilder,
              private httpClient: HttpClient,
              private router: Router,
  ) {
  }

  ngOnInit() {
    this.loginForm = this.fb.group({
      account: ['', [Validators.required]],
      pwd: ['', [Validators.required]],
      code: ['', [Validators.required]]
    });
    this.imgSrc = this.url + 'getVerify?' + new Date().getMilliseconds();
  }

  // 登录
  login(event, value) {
    console.log(value);
    this.httpClient.post(this.url + 'login', {
      account: value['account'],
      pwd: value['pwd'],
      code: value['code']
    }).subscribe(v => {
      if (v['code'] === 0) {
        this.router.navigate(['userList']);
      } else {
        alert('登录失败');
      }
    })
  }
  // 更换code
  changeCode() {
    this.imgSrc = this.url + 'getVerify?' + new Date();
  }
}
